/**
 * 
 */
package vn.tecviets.thuandt.controller.action.post;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vn.tecviets.thuandt.model.bo.PostBO;
import vn.tecviets.thuandt.model.common.HTMLFile;
import vn.tecviets.thuandt.model.form.LoginActionForm;

/*
 * DetelePostAction.java
 * 1.0
 * Mar 20, 2013 10:16:06 AM
 * 
 * Copyright notice
 *
 * Modification Logs:
 *
 * DATE					AUTHOR				DESCRIPTION
 * -----------------------------------------------------------
 * Mar 20, 2013			ThuanDT				Creation
 */

public class DeletePostAction extends Action {
	
	PostBO postBO = new PostBO();
	HTMLFile htmlFile = new HTMLFile();
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		/*
		 * Check login session
		 */
		LoginActionForm loginActionForm = (LoginActionForm) request.getSession().getAttribute("loginActionForm");
		if (loginActionForm == null) {
			return mapping.findForward("login");
		}
	
		try {
			
			/*
			 * If button delete is clicked
			 */
			if (null != request.getParameter("postId")) {
				int postId = Integer.parseInt(request.getParameter("postId"));
				String pathname = postBO.deletePost(postId);
				htmlFile.deleteFile(pathname);
			}
			
		} catch (NumberFormatException e) {
			e.printStackTrace();
			throw e;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

		return mapping.findForward("posts");
	}
}
